package com.vidyu.datalayer.dataaccesscomponents.impl;

import com.vidyu.businesslayer.businessentities.Contractor;
import com.vidyu.businesslayer.businessentities.Employer;
import com.vidyu.businesslayer.businessentities.Skill;
import com.vidyu.datalayer.dataaccesscomponents.ContractorDao;
import com.vidyu.datalayer.dataaccesscomponents.EmployerDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: eyalsa
 * Date: 28/02/12
 * Time: 14:38
 * Implementation Dao object for ContractorDao
 */

@SuppressWarnings({"unchecked", "UnusedDeclaration"})
@Repository("employerDaoDao")
public class EmployerDaoImpl extends BaseDaoImpl implements EmployerDao {

    @Autowired
    private HibernateTemplate template;

    @Override
    @Transactional(readOnly = false, propagation = Propagation.NESTED)
    public long save(Employer employer){
        return ((Long) template.save(employer));
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.NESTED)
    public void update(Employer employer){
        template.update(employer);
    }

    @Override
    public Employer findById(long id) {
        return template.get(Employer.class, id);
    }

    @Override
         public Employer getEmployerByEmail(String email){
        List<Employer> employers = this.findByNamedQuery("getEmployerByEmail", email);
        if (employers.isEmpty()) {
            return null;
        }
        return employers.get(0);
    }

    @Override
    public Employer getEmployerByEmailAndPassword(String email, String password){
        List<Employer> employers = this.findByNamedQuery("getEmployerByEmailAndPassword", email, password);
        if (employers.isEmpty()) {
            return null;
        }
        return employers.get(0);
    }
}
